package array

func isValidSudoku(board [][]byte) bool {
	rowsNum := make([][]int, 9)
	colsNum := make([][]int, 9)
	boxesNum := make([][]int, 9)

	for i := 0; i < 9; i++ {
		rowsNum[i] = make([]int, 9)
		colsNum[i] = make([]int, 9)
		boxesNum[i] = make([]int, 9)
	}

	for i := 0; i < 9; i++ {
		for j := 0; j < 9; j++ {

			if board[i][j] == '.' {
				continue
			}

			cruNum := board[i][j] - '0' // 将byte转为int

			if rowsNum[i][cruNum-1] != 0 {
				return false
			} else {
				rowsNum[i][cruNum-1]++
			}

			if colsNum[j][cruNum-1] != 0 {
				return false
			} else {
				colsNum[j][cruNum-1]++
			}

			var boxNum int
			boxNum = j/3 + (i/3)*3 // 用来判断在第几个3*3格

			if boxesNum[boxNum][cruNum-1] != 0 {
				return false
			} else {
				boxesNum[boxNum][cruNum-1]++
			}
		}
	}

	return true
}
